package com.oasystem.dao.jdbc.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.oasystem.dao.jdbc.BaseDAO;
import com.oasystem.domain.PagingBean;
import com.oasystem.domain.user.Permission;
import com.oasystem.domain.user.User;

public class UserDao extends BaseDAO {

	public User findUserByUserName(String userName) {
		String sql = "SELECT * FROM user WHERE name=?";
		return getJdbcTemplate().queryForObject(sql, new Object[] { userName },
				User.class);
	}

	public User findUserByUserId(String id) {
		String sql = "SELECT * FROM user u,permission p WHERE u.id=? and u.id = p.userid";
		return getJdbcTemplate().queryForObject(sql, new Object[] { id },
				new RowMapper<User>() {

					@Override
					public User mapRow(ResultSet rs, int arg1)
							throws SQLException {
						User user = new User();
						user.setLoginName(rs.getString("loginName"));
						user.setName(rs.getString("name"));
						user.setId(rs.getString("u.id"));
						user.setPermission(new Permission(rs
								.getString("p.permission")));
						return user;
					}

				});
	}

	public void userList(PagingBean<User> pagingBean) {
		String sql = "SELECT * FROM user u,permission p WHERE u.id = p.userid LIMIT ?,?";
		pagingBean.setData(getJdbcTemplate().query(
				sql,
				new Object[] { pagingBean.getCurrentRecordIndex(),
						pagingBean.getPageSize() }, new RowMapper<User>() {

					@Override
					public User mapRow(ResultSet rs, int index)
							throws SQLException {
						User user = new User();
						user.setLoginName(rs.getString("loginName"));
						user.setName(rs.getString("u.name"));
						user.setId(rs.getString("u.id"));
						user.setPermission(new Permission(rs
								.getString("p.permission")));
						return user;
					}

				}));
		sql = "SELECT COUNT(*) FROM user";
		pagingBean.setAllRecordCount(getJdbcTemplate().queryForInt(sql));

	}

	public void deleteUser(String id) {
		String sql = "DELETE FROM user WHERE id=?";
		getJdbcTemplate().update(sql, new Object[] { id });
		
		sql = "DELETE FROM permission WHERE userid=?";
		getJdbcTemplate().update(sql, new Object[] { id });
	}

	public void updateUser(User user) {
		String sql = null;
		Object[] params = null;
		int[] types = null;
		if (user.getPassword() == null || "".equals(user.getPassword())) {
			sql = "UPDATE user SET name=?,loginName=? WHERE id=?";
			params = new Object[] { user.getName(),
					user.getLoginName(), user.getId() };
			types = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR };
		} else {
			sql = "UPDATE user SET name=?,password=?,loginName=? WHERE id=?";
			params = new Object[] { user.getName(), user.getPassword(),
					user.getLoginName(), user.getId() };
			types = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
					Types.VARCHAR };
		}
		getJdbcTemplate().update(sql, params, types);
		
		sql = "update permission set permission=? where userId=?";
		getJdbcTemplate().update(sql, new Object[]{user.getPermission().getCode(),user.getId()});
	}

	public void saveUser(User user) {
		String sql = "INSERT INTO user (id,loginName,password,name) VALUES(?,?,?,?)";
		getJdbcTemplate().update(
				sql,
				new Object[] { user.getId(), user.getLoginName(),
						user.getPassword(), user.getName() });
		sql = "INSERT INTO permission (id,permission,userid) VALUES(?,?,?)";
		getJdbcTemplate().update(
				sql,
				new Object[] { user.getPermission().getPermissionID(),
						user.getPermission().getCode(), user.getId() });
	}

	public List<User> leaderList() {
		String sql = "select * from user u,permission p where u.id=p.userid and p.permission='L'";
		return getJdbcTemplate().query(sql, new RowMapper<User>() {

			@Override
			public User mapRow(ResultSet rs, int index) throws SQLException {
				User user = new User();
				user.setName(rs.getString("u.name"));
				user.setId(rs.getString("u.id"));
				return user;
			}

		});
	}
}
